#include <16F877A.h>

/*========================================== Controller Configuration Settings========================================*/
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include"lcd.h"

/*#define SELECT 0
#define UP PIN_C1
#define DOWN PIN_C0
#define EXIT PIN_C2
#define ENTER PIN_C3*/


#define RF_A PIN_C0
#define RF_B PIN_C1
#define RELAY PIN_C2
#define MOTOR PIN_C3
#define BUZ PIN_C5


byte const a[4][3]={'1','2','3','4',
							  '5','6','7','8',
							  '9','0','a','b'};

unsigned char tripdata=0,ch,otp;

//*******************************************************************************

void buzzer(unsigned char loop, int tone)
{
   unsigned char beep;
   for(beep=0;beep<loop;beep++)
   {
      output_high(BUZ);
      delay_ms(tone);
      output_low(BUZ);
      delay_ms(tone);
   }
}

//******************************************************************
unsigned char kb()
  {
 unsigned char gg,Rowdt,resul=45,i;

while(resul==45)
{
	tripdata++;
  	if(tripdata>=100) tripdata=0;
      Rowdt = 0b11111110;
      gg=0;
      for(i=0;i<5;i++)
      {
         output_a(Rowdt);
         delay_ms(10);
         gg = input_b();
         delay_ms(10);

               if((gg&0x10)==0)
               {
                  resul=1+(i*3);
               }
               else if((gg&0x20)==0)               
               {
                  resul=2+(i*3);
               }
               else if((gg&0x40)==0) 
               {
                  resul=3+(i*3);
               }
               if(resul==10) {resul=45;}
               if(resul==11) {resul=0;}
               if(resul==12) {resul=45;}

         if(resul!=45)
            {
            display(val[resul]);            
            delay_ms(1500);
            return(resul);
            }
         Rowdt=(Rowdt<<1)+1;
     }

}
  return(resul);
}

//***********************************************************************************

void sendotp(unsigned char ab)
{
unsigned char q;
otp=ab;

 	printf("at+cmgf=1\n");
      putc(0x0d);
      putc(0x0a);
      delay_ms(100);
	   printf("at+cmgs=\"+918015766605\"\n");
      putc(0x0d);
      putc(0x0a);
      delay_ms(100);
	   printf("%u Is OTP for operating to unlock Boat",ab);
      putc(26);
      delay_ms(1000);
}

//***********************************************************************************

void main( void )
{
	unsigned char test,tr;
   port_b_pullups(true);
	tripdata=28;
  	while(1)
  	{
	LCD_init();
  		LCD_row1();
  		display("    MARINE");
  		LCD_row2();
  		display("  ALERT SYSTEM");
  	delay_ms(100);
  	buzzer(3,1000);


	tripdata++;
	LCD_delay(500);

	if(input(RF_A)==0)
   	{
  		LCD_clear();
  		LCD_row1();
  		display("!!! ALERT !!!!");
  		LCD_row2();
  		display("U R ABT 2 CROSS");
  		LCD_delay(100);
   		buzzer(15,50);
	}

	if(input(RF_B)==0)
   	{
  		LCD_clear();
  		LCD_row1();
  		display("U HAV CROSSED");
  		LCD_row2();
  		display("UR BOAT LOCKED");
  		LCD_delay(100);
   		buzzer(5,2000);
		sendotp(++tripdata);
		tr=0;
		while(tr==0)
		{
				tripdata++;
  			LCD_clear();
  			LCD_row1();
  			display("ENTER OTP:");
			ch=45;
		//	while(ch==45){ch=kb();tripdata++;}
		//	test=(ch*10);
      test=((kb())*10);
			ch=45;
		//	while(ch==45){ch=kb();tripdata++;}
			test=test+(kb());
			tripdata++;
			delay_ms(500);
   			if(test==otp)
   			{
  				tr=1;
  		           LCD_clear();
  		           LCD_row1();
  		           display("  UR BOAT");
  		           LCD_row2();
  		           display("  UNLOCKED");
  		           delay_ms(3000);
  			   }
            else
            {
  		           LCD_clear();
  			        LCD_row2();
  			        display("Invalid OTP");
  		           delay_ms(3000);           
            }
		}
      }

      if(tripdata>=100) tripdata=0;
	}
}
